package cn.qzzg.service.impl;

import cn.qzzg.common.exceptlion.CommonException;
import cn.qzzg.dto.PagingQueryDTO;
import cn.qzzg.dto.TransferBoxDTO;
import cn.qzzg.enums.CodeEnum;
import cn.qzzg.mapper.TransferBoxMapper;
import cn.qzzg.pojo.TransferBox;
import cn.qzzg.service.TransferBoxService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 九九
 * @description 针对表【transfer_box(转运箱表)】的数据库操作Service实现
 * @createDate 2023-03-25 21:51:39
 */
@Service
public class TransferBoxServiceImpl extends ServiceImpl<TransferBoxMapper, TransferBox>
        implements TransferBoxService {
    /**
     * 通用条件构造器
     */
    private LambdaQueryWrapper<TransferBox> wrapper;

    /**
     * 查询所有数据
     *
     * @return 结果对象集合
     */
    @Override
    public List<TransferBox> findAll() {
        wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(TransferBox::getCreateTime);
        return list(wrapper);
    }

    /**
     * 通过ID查询一条数据
     *
     * @param objId 需要查询的对象ID
     * @return 结果对象
     */
    @Override
    public TransferBox findById(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        return getById(objId);
    }

    /**
     * 添加一条数据
     *
     * @param obj 需要添加的对象
     * @return 添加的对象ID
     */
    @Override
    public String saveOne(TransferBox obj) {
        if (ObjectUtils.isEmpty(obj))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(obj.getTransferBoxId()), TransferBox::getTransferBoxId, obj.getTransferBoxId());
        if (count(wrapper) > 0)
            throw new CommonException(CodeEnum.CUSTOM_ERROR_F0101);
        save(obj);
        return obj.getTransferBoxId();
    }

    /**
     * 通过ID删除一条数据
     *
     * @param objId 需要删除的对象ID
     * @return 删除是否成功
     */
    @Override
    public boolean removeOne(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        removeById(objId);
        return true;
    }

    /**
     * 多条件分页查询
     *
     * @param queryDTO 查询条件与分页属性
     * @return 分页查询数据结果
     */
    @Override
    public Page<TransferBox> findPage(PagingQueryDTO<TransferBoxDTO> queryDTO) {
        wrapper = new LambdaQueryWrapper<>();
        TransferBoxDTO transferBoxDTO = queryDTO.getData();
        if (ObjectUtils.isNotEmpty(transferBoxDTO)) {
            wrapper.eq(ObjectUtils.isNotEmpty(transferBoxDTO.getTransferBoxId()), TransferBox::getTransferBoxId, transferBoxDTO.getTransferBoxId())
                    .eq(ObjectUtils.isNotEmpty(transferBoxDTO.getSampleSourceId()), TransferBox::getSampleSourceId, transferBoxDTO.getSampleSourceId())
                    .eq(ObjectUtils.isNotEmpty(transferBoxDTO.getDictionaryInfoId()), TransferBox::getDictionaryInfoId, transferBoxDTO.getDictionaryInfoId())
                    .like(ObjectUtils.isNotEmpty(transferBoxDTO.getInspectionPersonnel()), TransferBox::getInspectionPersonnel, transferBoxDTO.getInspectionPersonnel())
                    .like(ObjectUtils.isNotEmpty(transferBoxDTO.getInspectionPersonnelMobile()), TransferBox::getInspectionPersonnelMobile, transferBoxDTO.getInspectionPersonnelMobile())
                    .orderByDesc(TransferBox::getCreateTime);

            List<String> periodTime = transferBoxDTO.getPeriodTime();
            if (periodTime != null && periodTime.size() != 0)
                wrapper.ge(TransferBox::getCreateTime, periodTime.get(0))   // 创建时间大于等于
                        .le(TransferBox::getCreateTime, periodTime.get(1)); // 创建时间小于等于

        }
        Page<TransferBox> transferBoxPage = new Page<>();
        transferBoxPage.setSize(queryDTO.getPage().getSize())
                .setCurrent(queryDTO.getPage().getCurrent());

        return page(transferBoxPage, wrapper);
    }
}




